/* 适配器 */
import { LOGO, ORDER_STATUS } from '@/static/staticData'
import { filterVal } from './index'
import { renderSize } from './file'
import Router from './link'
import timeFormat from '@/uview-ui/libs/function/timeFormat'
/**
 * 适配消息列表
 */
export function adaMessages(data, scheme) {
  return {
    id: data.id || (data.latestMessage && data.latestMessage.commId) || '',
    scheme,
    avatar: data.avatar || data.chatterUserInfo.avatarUrl || LOGO,
    name: data.name || data.chatterUserInfo.nickname || 'Aha小萌新',
    updateTime: data.updateTime || data.latestMessage.updateTime || Date.now(),
    content: data.content || data.latestMessage.context || '',
    type: data.latestMessage.type || 'text',
    unreadCount: data.unreadCount || 0,
    withdraw: data.latestMessage.withdraw || false
  }
}

/**
 * 项目卡片
 * @param {Object} project 项目粗略map
 * @param {Boolean} showMembers 是否展示成员，如果展示成员则不展示比赛名
 */
export async function adaptProjectCard(project, showMembers = false) {
  let type // 项目类型

  /* 获取高亮标签，前三成员或者课程名 */
  let highlightTags = ''

  if (project.id < 500000) { // 竞赛项目的id小于500000
    type = 'competition'
    if (showMembers) {
      highlightTags = project.threeMembers ? (project.threeMembers.map(item => item.memberUser.nickname)).join(' ') : ''
    } else {
      highlightTags = project.compName
    }
  } else {
    type = 'courseDesign'
    highlightTags = project.courseName
  }

  return {
    type,
    id: project.id,
    compId: project.compId, // 参与的比赛id
    name: filterVal(project.name, project.name), // 项目名称
    avatarUrl: filterVal(project.avatarUrl, project.avatarUrl), // 团队头像
    highlightTags, // 高亮标签,
    tags: filterVal(project.tags, project.tags), // 标签
    passed: filterVal(project.passed, true), // 是否通过
    read: filterVal(project.read, project.read), // 阅读量
    collect: filterVal(project.collect, project.collect), // 阅读量
    time: filterVal(project.awardTime, timeFormat(project.createTime, 'yyyy-mm-dd')), // 展示时间
  }
}

/**
 * 编辑项目信息——项目的基本信息
 */
export async function projectBaseInfo(project) {
  return {
    ...project,
    id: filterVal(project.id, project.cdId),
    name: filterVal(project.name, project.cdName), // 项目名称
    avatarUrl: filterVal(project.avatarUrl, project.cdAvatarUrl), // 团队头像
    tags: filterVal(project.tags, project.cdTags), // 标签
    intro: filterVal(project.intro, project.cdIntro), // 描述
  }
}

/**
 * 项目详情
 */
export async function adaptiveProjectDetail(project) {
  return {
    ...project,
    id: filterVal(project.id, project.cdId),
    isAnonymous: filterVal(project.isAnonymous, false), // 是否是匿名项目
    name: filterVal(project.name, project.cdName), // 项目名称
    avatarUrl: filterVal(project.avatarUrl, project.cdAvatarUrl), // 团队头像
    tags: filterVal(project.tags, project.cdTags), // 标签
    passed: filterVal(project.passed, project.cdPassed), // 是否通过
    read: filterVal(project.read, project.cdRead), // 阅读量
    collect: filterVal(project.collect, project.cdCollect), // 阅读量
    time: filterVal(project.awardTime, project.cdCreateTime), // 展示时间
    members: project.members ? project.members.sort((a, b) => a.rank - b.rank) : []
  }
}

/**
 * 选择文件适配，从图库选择和从微信选择
 */
export function adaptiveSelectFile(file) {
  const data = {
    name: file.name,
    size: file.size,
    sizeText: renderSize(file.size),
    type: file.type || file.fileType,
    path: file.path || file.tempFilePath
  }

  data.name = data.name ? data.name.split('.')[ 0 ] : data.type // 没有name就取type做name
  data.suffix = data.path.split('.')[ 1 ] // 尾缀
  return data
}

/**
 * 订单粗略信息
 */
export function adaptiveOrderCard(order) {
  const params = {
    id: order.id,
    time: timeFormat(order.createTime, 'yyyy.mm.dd hh:MM'),
    totalExpend: order.totalCost,
    status: ORDER_STATUS[ order.status ],
    title: '', // 主标题
    subhead: `${ ORDER_STATUS[ order.status ].label } - `, // 副标题
    clickCb: () => {}, // 点击回调
  }
  /* 根据不同类型订单整理 */

  if (order.project && order.orderResources[ 0 ]) { // 竞赛项目
    params.title = order.orderResources[ 0 ].resource.name
    params.subhead += order.project.name
    params.clickCb = () => Router.navigateToProject(order.project.id)
  } else if (order.courseDesign && order.orderResources[ 0 ]) { // 课设项目
    params.title = order.orderResources[ 0 ].resource.name
    params.subhead += order.courseDesign.name
    params.clickCb = () => Router.navigateToProject(order.courseDesign.id)
  } else if (order.seniorOrder) { // 学长学姐咨询
    params.title = '学长学姐咨询'
    params.subhead += `${ order.seniorOrder.seniorInfoRoughVo.siNickName } - ${ order.seniorOrder.seniorOrderRoughVo.soOrderTime.split('|').length * 30 }分钟` // 姓名 - 时长
    params.clickCb = () => Router.navigateToConsultList()
  } else {
    params.title = '订单丢失'
    params.subhead += '丢失'
    params.clickCb = () => {}
  }
  return params
}
